package iodemo;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileByte
{
    public static void main (String[] args)
    {
        long time1 = System.currentTimeMillis();
        
        copyFile("D:/iDonotKnow.txt", "D:/NoYouKnow.txt");
        
        long time2 = System.currentTimeMillis();
    
        System.out.println(time2 - time1);
    }
    
    /**
     *
     * @param source        源文件
     * @param destination   目的地
     *
     * 要文件关闭文件时，后创建的文件要先关闭
     */
    public static void copyFile(String source, String destination)
    {
        //按照IO流对象被创建的逆序来关闭
        try(FileInputStream fileInputStream = new FileInputStream(source);
            FileOutputStream fileOutputStream = new FileOutputStream(destination))
        {
            //开始操作
            //创建字节缓冲区
            
            byte[] buffers = new byte[1024];
            
            int temp = 0;
            
            while((temp = fileInputStream.read(buffers)) != -1)
            {
                fileOutputStream.write(buffers, 0, temp);
            }
            
            //将数据从内存中写出到磁盘中
            fileOutputStream.flush();
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
    }
}
